import React, { Component } from "react";

class Child extends Component {
  state = {
    nowTime: 1,
  };
  timer = null;
  componentDidMount() {
    this.timer = setInterval(() => {
      console.log(1111);
      this.setState({ nowTime: this.state.nowTime + 1 });
    }, 1000);
  }
  componentWillUnmount() {
    clearInterval(this.timer);
  }
  render() {
    return <div>{this.state.nowTime}</div>;
  }
}

class App extends Component {
  state = {
    showChild: true,
  };
  render() {
    return (
      <div>
        {this.state.showChild && <Child />}
        <button onClick={() => this.setState({ showChild: false })}>
          隐藏
        </button>
      </div>
    );
  }
}

export default App;
